package org.swingDemo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class DesktopDemo extends JFrame {

    JButton btnLaunchApplication = new JButton("Launch Application");
    JButton btnLaunchBrowser = new JButton("Launch Browser");
    JButton btnLaunchEmail = new JButton("Launch Email");
    JRadioButton rbEdit = new JRadioButton("Edit");
    JRadioButton rbOpen = new JRadioButton("Open", true);
    JRadioButton rbPrint = new JRadioButton("Print");
    JTextField txtBrowserURI = new JTextField();
    JTextField txtMailTo = new JTextField();
    JTextField txtFile = new JTextField();
    ButtonGroup bgAppAction = new ButtonGroup();
    JLabel lblMailRecipient = new JLabel("E-mail:");
    JLabel lblBrowserUri = new JLabel("URI:");
    JLabel lblFile = new JLabel("File:");
    JButton btnFile = new JButton("...");
    JLabel emptyLabel = new JLabel(" ");
    JPanel conLeft = new JPanel();
    JPanel conCenter = new JPanel();
    JPanel conRight = new JPanel();
    JFileChooser fc = new JFileChooser();
    File file;

    private Desktop desktop;
    private Desktop.Action action = Desktop.Action.OPEN;

    public DesktopDemo() {
        initComponents();

        disableActions();

        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            enableSupportedActions();
        }
        loadFrameIcon();
        setResizable(false);
    }

    private void initComponents() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("DesktopDemo");
        setLocationRelativeTo(null);

        txtBrowserURI.addActionListener(e -> {
            onLaunchBrowser(null);
        });

        btnLaunchBrowser.addActionListener(e -> {
            onLaunchBrowser(e);
        });

        txtMailTo.addActionListener(e -> {
            onLaunchMail(null);
        });

        btnLaunchEmail.setText("Launch Mail");
        btnLaunchEmail.addActionListener(e -> {
            onLaunchMail(e);
        });

        txtFile.addActionListener(e -> {
            onLaunchDefaultApplication(null);
        });

        rbOpen.addActionListener(e -> {
            onOpenAction(e);
        });

        rbEdit.addActionListener(e -> {
            onEditAction(e);
        });

        rbPrint.addActionListener(e -> {
            onPrintAction(e);
        });

        btnLaunchApplication.addActionListener(e -> {
            onLaunchDefaultApplication(e);
        });

        btnFile.addActionListener(e -> {
            onChooseFile(e);
        });

        Container conFrame = this.getContentPane();

        bgAppAction.add(rbOpen);
        bgAppAction.add(rbEdit);
        bgAppAction.add(rbPrint);

        GroupLayout layout = new GroupLayout(conFrame);
        conFrame.setLayout(layout);
        layout.setAutoCreateContainerGaps(true);
        layout.setAutoCreateGaps(true);

        GroupLayout.SequentialGroup majorHGroup = layout.createSequentialGroup();

        // Horizontal Group
        GroupLayout.ParallelGroup lblHGroup = layout.createParallelGroup(
                GroupLayout.Alignment.LEADING);
        lblHGroup.addComponent(lblBrowserUri, GroupLayout.Alignment.LEADING);
        lblHGroup.addComponent(lblMailRecipient, GroupLayout.Alignment.TRAILING);
        lblHGroup.addComponent(lblFile, GroupLayout.Alignment.TRAILING);

        GroupLayout.ParallelGroup txtFieldsHGroup = layout.createParallelGroup(
                GroupLayout.Alignment.LEADING);
        txtFieldsHGroup.addComponent(txtMailTo);
        txtFieldsHGroup.addComponent(txtBrowserURI);
        GroupLayout.SequentialGroup rbHGroup = layout.createSequentialGroup();
        rbHGroup.addComponent(rbOpen);
        rbHGroup.addComponent(rbEdit);
        rbHGroup.addComponent(rbPrint);
        txtFieldsHGroup.addGroup(rbHGroup);
        GroupLayout.SequentialGroup fileHGroup = layout.createSequentialGroup();
        fileHGroup.addComponent(txtFile);
        fileHGroup.addComponent(btnFile);
        txtFieldsHGroup.addGroup(fileHGroup);

        GroupLayout.ParallelGroup btnHGroup = layout.createParallelGroup(
                GroupLayout.Alignment.LEADING);
        btnHGroup.addComponent(btnLaunchBrowser);
        btnHGroup.addComponent(btnLaunchEmail);
        btnHGroup.addComponent(btnLaunchApplication);

        majorHGroup.addGroup(lblHGroup);
        majorHGroup.addGroup(txtFieldsHGroup);
        majorHGroup.addGroup(btnHGroup);

        layout.setHorizontalGroup(majorHGroup);

        // Vertical Group
        GroupLayout.SequentialGroup majorVGroup = layout.createSequentialGroup();

        GroupLayout.ParallelGroup uriVGroup = layout.createParallelGroup(
                GroupLayout.Alignment.BASELINE);
        uriVGroup.addComponent(lblBrowserUri);
        uriVGroup.addComponent(txtBrowserURI);
        uriVGroup.addComponent(btnLaunchBrowser);

        GroupLayout.ParallelGroup mailVGroup = layout.createParallelGroup(
                GroupLayout.Alignment.BASELINE);
        mailVGroup.addComponent(lblMailRecipient);
        mailVGroup.addComponent(txtMailTo);
        mailVGroup.addComponent(btnLaunchEmail);

        GroupLayout.ParallelGroup rbVGroup = layout.createParallelGroup(
                GroupLayout.Alignment.BASELINE);
        rbVGroup.addComponent(rbOpen);
        rbVGroup.addComponent(rbEdit);
        rbVGroup.addComponent(rbPrint);

        GroupLayout.ParallelGroup fileVGroup = layout.createParallelGroup(
                GroupLayout.Alignment.BASELINE);
        fileVGroup.addComponent(lblFile);
        fileVGroup.addComponent(btnLaunchApplication);
        fileVGroup.addComponent(txtFile);
        fileVGroup.addComponent(btnFile);

        majorVGroup.addGroup(uriVGroup);
        majorVGroup.addGroup(mailVGroup);
        majorVGroup.addGroup(rbVGroup);
        majorVGroup.addGroup(fileVGroup);

        layout.setVerticalGroup(majorVGroup);

        pack();
    }

    private void loadFrameIcon() {
        URL imgUrl = null;
        ImageIcon imgIcon = null;

        imgUrl = DesktopDemo.class.getResource("/images/desk32.gif");
        imgIcon = new ImageIcon(imgUrl);
        Image image = imgIcon.getImage();
        this.setIconImage(image);
    }

    private void onPrintAction(ActionEvent evt) {
        action = Desktop.Action.PRINT;
    }

    private void onEditAction(ActionEvent evt) {
        action = Desktop.Action.EDIT;
    }

    private void onOpenAction(ActionEvent evt) {
        action = Desktop.Action.OPEN;
    }

    private void onLaunchDefaultApplication(ActionEvent evt) {
        String fileName = txtFile.getText();
        File file = new File(fileName);

        try {
            switch (action) {
                case OPEN:
                    desktop.open(file);
                    break;
                case EDIT:
                    desktop.edit(file);
                    break;
                case PRINT:
                    desktop.print(file);
                    break;
            }
        } catch (IOException ex) {
            System.out.println("Cannot perform the given operation to the " +
                    file + " file");
        }
    }

    private void onLaunchMail(ActionEvent evt) {
        String mailTo = txtMailTo.getText();
        URI uriMailTo = null;
        try {
            if (mailTo.length() > 0) {
                uriMailTo = new URI("mailto", mailTo, null);
                desktop.mail(uriMailTo);
            } else {
                desktop.mail();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (URISyntaxException use) {
            use.printStackTrace();
        }
    }

    private void onLaunchBrowser(ActionEvent evt) {
        URI uri = null;
        try {
            uri = new URI(txtBrowserURI.getText());
            desktop.browse(uri);
        } catch (IOException ioe) {
            System.out.println("The system cannot find the " + uri + " file specified");
        } catch (URISyntaxException use) {
            System.out.println("Illegal character in path");
        }
    }

    private void onChooseFile(ActionEvent evt) {
        if (evt.getSource() == btnFile) {
            int returnVal = fc.showOpenDialog(DesktopDemo.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                txtFile.setText(file.getAbsolutePath());
            }
        }
    }

    private void enableSupportedActions() {
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            txtBrowserURI.setEnabled(true);
            btnLaunchBrowser.setEnabled(true);
        }

        if (desktop.isSupported(Desktop.Action.MAIL)) {
            txtMailTo.setEnabled(true);
            btnLaunchEmail.setEnabled(true);
        }

        if (desktop.isSupported(Desktop.Action.OPEN)) {
            rbOpen.setEnabled(true);
        }

        if (desktop.isSupported(Desktop.Action.EDIT)) {
            rbEdit.setEnabled(true);
        }

        if (desktop.isSupported(Desktop.Action.PRINT)) {
            rbPrint.setEnabled(true);
        }

        if (rbEdit.isEnabled() || rbOpen.isEnabled() || rbPrint.isEnabled()) {
            txtFile.setEnabled(true);
            btnLaunchApplication.setEnabled(true);
            btnFile.setEnabled(true);
        }
    }

    private void disableActions() {
        txtBrowserURI.setEnabled(false);
        btnLaunchBrowser.setEnabled(false);

        txtMailTo.setEnabled(false);
        btnLaunchEmail.setEnabled(false);

        rbOpen.setEnabled(false);
        rbEdit.setEnabled(false);
        rbPrint.setEnabled(false);

        txtFile.setEnabled(false);
        btnLaunchApplication.setEnabled(false);
        btnFile.setEnabled(false);
    }
}
